package com.easylinkin.linkappapi.zncd.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.service.DeviceService;
import com.easylinkin.linkappapi.deviceattributestatus.entity.DeviceAttributeStatus;
import com.easylinkin.linkappapi.deviceattributestatus.service.DeviceAttributeStatusService;
import com.easylinkin.linkappapi.devicetype.entity.DeviceType;
import com.easylinkin.linkappapi.devicetype.service.DeviceTypeService;
import com.easylinkin.linkappapi.deviceunit.entity.DeviceUnit;
import com.easylinkin.linkappapi.deviceunit.service.DeviceUnitService;

import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

@RestController
public class DeviceZncdController {
	
	@Resource
    private DeviceService deviceService;

	@Resource
    private DeviceUnitService deviceUnitService;
	
	@Resource
    private DeviceTypeService deviceTypeService;
	@Resource
    private DeviceAttributeStatusService deviceAttributeStatusService;
	
	/**
	 * 中南财大小程序接口
	 */
	@PostMapping("/zncd/selectDeviceByCode")
	public RestMessage selectDeviceByCode(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Device device) {
		Assert.notNull(device.getCode(), "设备编号不能为空");
		QueryWrapper<Device> wrapper = new QueryWrapper<Device>();
		wrapper.eq("code", device.getCode());
		wrapper.eq("delete_state", 1);
		device = deviceService.getOne(wrapper);
		if(device!=null) {
			QueryWrapper<DeviceUnit> duwrapper = new QueryWrapper<DeviceUnit>();
			duwrapper.eq("id", device.getDeviceUnitId());
			duwrapper.eq("delete_state", 1);
			DeviceUnit deviceUnit = deviceUnitService.getOne(duwrapper);
			device.setDeviceUnit(deviceUnit);
			QueryWrapper<DeviceType> dtwrapper = new QueryWrapper<DeviceType>();
			dtwrapper.eq("id", deviceUnit.getDeviceTypeId());
			dtwrapper.eq("delete_state", 1);
			DeviceType deviceType = deviceTypeService.getOne(dtwrapper);
			device.setDeviceType(deviceType);
			return RestBuilders.successBuilder().data(device).build();
		}
		return RestBuilders.successBuilder().data(device).message("设备编号不存在").build();
	}

	/**
	 * 中南财大小程序接口
	 */
	@PostMapping("/zncd/selectDeviceRealtimeDataByCode")
	public RestMessage selectDeviceDeviceRealtimeDataByCode(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Device device) {
		Assert.notNull(device.getCode(), "设备编号不能为空");
		QueryWrapper<Device> wrapper = new QueryWrapper<Device>();
		wrapper.eq("code", device.getCode());
		wrapper.eq("delete_state", 1);
		device = deviceService.getOne(wrapper);
		if(device!=null) {
			QueryWrapper<DeviceUnit> duwrapper = new QueryWrapper<DeviceUnit>();
			duwrapper.eq("id", device.getDeviceUnitId());
			DeviceUnit deviceUnit = deviceUnitService.getOne(duwrapper);
			DeviceAttributeStatus deviceAttributeStatus = new DeviceAttributeStatus();
			deviceAttributeStatus.setDeviceCode(device.getCode());
			deviceAttributeStatus.setVersion(deviceUnit.getVersion());
			
			List<DeviceAttributeStatus> list = deviceAttributeStatusService.getDeviceRealtimeData(deviceAttributeStatus);
			return RestBuilders.successBuilder().data(list).build();
		}
		return RestBuilders.successBuilder().data(device).message("设备编号不存在").build();
	}

}
